//package com.weirblog.resource;
//
//import java.io.File;
//import java.io.FileOutputStream;
//import java.io.InputStream;
//import java.io.OutputStream;
//import java.util.Date;
//import java.util.Random;
//
//import javax.enterprise.context.ApplicationScoped;
//import javax.transaction.Transactional;
//import javax.ws.rs.Consumes;
//import javax.ws.rs.DELETE;
//import javax.ws.rs.FormParam;
//import javax.ws.rs.GET;
//import javax.ws.rs.POST;
//import javax.ws.rs.Path;
//import javax.ws.rs.PathParam;
//import javax.ws.rs.Produces;
//import javax.ws.rs.QueryParam;
//import javax.ws.rs.core.Context;
//import javax.ws.rs.core.MediaType;
//
//import org.apache.commons.lang3.StringUtils;
//import org.eclipse.microprofile.config.inject.ConfigProperty;
//import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;
//
//import com.weirblog.entity.Photo;
//import com.weirblog.entity.PhotoImg;
//import com.weirblog.util.BaseUtil;
//import com.weirblog.vo.DataGrid;
//import com.weirblog.vo.FileBody;
//import com.weirblog.vo.JsonVO;
//
//import io.quarkus.hibernate.orm.panache.PanacheQuery;
//import io.quarkus.panache.common.Sort;
//import io.quarkus.panache.common.Sort.Direction;
//import io.quarkus.qute.Template;
//import io.quarkus.qute.TemplateInstance;
//import io.quarkus.qute.api.ResourcePath;
//import io.vertx.core.http.HttpServerRequest;
//
//@Path("photo")
//@ApplicationScoped
//public class PhotoController {
//	
//	@ConfigProperty(name = "photo.img.save-path")
//	String photoImgPath;
//	
//	@Context
//    HttpServerRequest request;
//	
//	@ResourcePath("admin/photoAdd.html")
//	Template photoAdd;
//	@ResourcePath("admin/photoUpload.html")
//	Template photoUpload;
//
//	@GET
//	@Path("uploadUI/{id}")
//	public TemplateInstance photoUpload(@PathParam("id") Integer id) {
//		return photoUpload.data("photo", new Photo(id));
//	}
//	@GET
//	@Path("addUI")
//	public TemplateInstance photoAdd() {
//		return photoAdd.data("photo", null);
//	}
//	@GET
//	@Path("editUI/{id}")
//	public TemplateInstance photoEdit(@PathParam("id") Integer id) {
//		Photo photo = Photo.findById(id);
//		return photoAdd.data("photo", photo);
//	}
//
//	@GET
//	@Path("list")
//	public DataGrid<Photo> list(@QueryParam("page") Integer page, @QueryParam("rows") Integer rows) {
//		PanacheQuery<Photo> pageQuery = Photo.findAll(Sort.by("createDate", Direction.Descending)).page(page-1, rows);
//		DataGrid<Photo> dataGrid = new DataGrid<Photo>(pageQuery.count(), pageQuery.list());
//		return dataGrid;
//	}
//	
//	@POST
//	@Path("listImg/{photoId}")
//	public DataGrid<PhotoImg> listImg(@PathParam("photoId") Integer photoId,PhotoImg photoImg) {
//		PanacheQuery<PhotoImg> pageQuery = PhotoImg.find("photo_id = ?1", photoId).page(photoImg.page, photoImg.rows);
//		DataGrid<PhotoImg> dataGrid = new DataGrid<PhotoImg>(pageQuery.count(), pageQuery.list());
//		return dataGrid;
//	}
//	@POST
//	@Path("addImg/{id}")
//	public JsonVO<PhotoImg> addImg(@PathParam("id") Integer id,PhotoImg photoImg) {
//		JsonVO<PhotoImg> j = new JsonVO<>();
//		
//		PhotoImg entity = PhotoImg.findById(id);
//		entity.imgDesc = photoImg.imgDesc;
//		j.setMsg("修改成功");
//		j.setSuccess(true);
//		return j;
//	}
//	
//	@POST
//	@Path("add")
//	@Transactional
//	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
//	public JsonVO<Object> add(
//			@FormParam("id") String id,
//			@FormParam("name") String name) {
//		if (StringUtils.isBlank(id)) {
//			Photo entity = new Photo();
//			entity.name = name;
//			entity.persistAndFlush();
//		}else {
//			Photo entity = Photo.findById(Integer.valueOf(id));
//			entity.name = name;
//			entity.updateDate = new Date();
//		}
//		JsonVO<Object> json = new JsonVO<>();
//		json.setMsg("添加/修改成功");
//		json.setSuccess(true);
//		return json;
//	}
//	
//	@DELETE
//	@Path("delete")
//	@Transactional
//	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
//	public JsonVO<Object> delete(@FormParam("photoIds") String photoIds) {
//		String[] ids = photoIds.split(",");
//		for (String id : ids) {
//			Photo.deleteById(Integer.valueOf(id));
//		}
//		JsonVO<Object> json = new JsonVO<>();
//		json.setMsg("相册删除成功");
//		json.setSuccess(true);
//		return json;
//	}
//	
//	@POST
//    @Path("/uploadImg/{id}")
//    @Produces(MediaType.TEXT_PLAIN)
//    @Consumes(MediaType.MULTIPART_FORM_DATA)
//	public JsonVO<Object> uploadImg(@PathParam("id") Integer id, 
//			@MultipartForm FileBody fileBody
//			) {
//		String savePath = photoImgPath + "photo/"+id+"/";
//		File uploadDir = new File(savePath);
//		if (!uploadDir.exists()) {
//			uploadDir.mkdirs();
//		}
//		JsonVO<Object> json = new JsonVO<>();
//		String fileName = fileBody.filePath;
//		String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
//		try {
//			InputStream i = fileBody.inputStream.getBody(InputStream.class, null);
//			OutputStream o = new FileOutputStream(new File(savePath + BaseUtil.parseDateToStringNomm(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt));
//            i.transferTo(o);
//            i.close();
//            o.close();
//			json.setSuccess(true);
//		} catch (Exception e) {
//			json.setMsg("文件上传失败");
//			e.printStackTrace();
//		}
//		return json;
//	}
////	@RequestMapping("/view")
////	public String view(Integer id,HttpServletRequest request,Model model) {
////		ServletContext application = request.getSession().getServletContext();
////		String savePath = application.getRealPath("/") + "/attached/photo/thumbnail/"+id+"/";
////		File uploadDir = new File(savePath);
////		File[] fs = uploadDir.listFiles();
////		List<String> list = new ArrayList<String>();
////		if (fs!=null && fs.length>0) {
////			for (File file : fs) {
////				list.add(id.toString()+"/"+file.getName());
////			}
////		}
////		model.addAttribute("imgs", list);
////		model.addAttribute("id", id);
////		return "/WEB-INF/blog/photo/photo_view";
////	}
////	@RequestMapping("/topImg")
////	@ResponseBody
//	@POST
//	@Path("topImg")
//	public JsonVO topImg(Integer id,Photo photo) {
//		JsonVO j = new JsonVO();
////		ServletContext application = request.getSession().getServletContext();
////		String savePath = application.getRealPath("/") + "/attached/photo/"+topImg;
//		String savePath = photoImgPath + "/attached/photo/"+photo.thumbnail;
//		File uploadDir = new File(savePath);
//		try {
//			Photo p = Photo.findById(id);
//			p.thumbnail = uploadDir.getName();
//			p.updateDate = new Date();
////			photoService.save(p);
////			ImageUtil.resize(uploadDir, new File(application.getRealPath("/") + "/attached/"+uploadDir.getName()),800, 0.7f);
//			j.setMsg("设置成功");
//			j.setSuccess(true);
//		} catch (Exception e) {
//			e.printStackTrace();
//			j.setMsg("设置失败");
//		}
//		return j;
//	}
////	@RequestMapping("/deletePhoto")
////	@ResponseBody
//	@DELETE
//	@Path("deletePhoto")
//	public JsonVO deletePhoto(PhotoImg photoImg) {
////		ServletContext application = request.getSession().getServletContext();
//		JsonVO j = new JsonVO();
//		try {
////			PhotoImg pImg = photoImgService.find(topImg);
//			PhotoImg pImg = PhotoImg.find("imgPath = ?1", photoImg.imgPath).firstResult();
//			if (pImg!=null) {
////				photoImgService.delete(pImg);
//				PhotoImg.deleteById(pImg.id);
//			}
//			File file = new File(photoImgPath + "attached\\photo\\thumbnail\\"+photoImg.imgPath);
//			if (file.exists() && file.isFile()) {
//				file.delete();
//			}
//			j.setMsg("图片删除成功");
//			j.setSuccess(true);
//		} catch (Exception e) {
//			e.printStackTrace();
//			j.setMsg("图片删除失败");
//		}
//		return j;
//	}
//	
////	@RequestMapping("/eye_main/{id}")
////	public String eyeMain(@PathVariable Integer id,Model model) {
////		model.addAttribute("id", id);
////		return "/WEB-INF/eye_main";
////	}
////	@RequestMapping("/eye/{id}")
////	public String eye(@PathVariable Integer id,Model model) {
////		List<PhotoImg> pis = photoImgService.getListByPhotoId(id);
////		model.addAttribute("photos", pis);
////		return "/WEB-INF/eye";
////	}
////	@RequestMapping("/eyePhone/{id}")
////	public String eyePhone(@PathVariable Integer id,Model model) {
////		List<PhotoImg> pis = photoImgService.getListByPhotoId(id);
////		model.addAttribute("photos", pis);
////		return "/WEB-INF/eye_phone";
////	}
//}
